package com.zq.zqplayer.mvvm.recommend

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Toast
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import com.ziq.base.base.BaseFragment
import com.ziq.base.extension.binding
import com.zq.customviewlib.AutoRollViewPager
import com.zq.zqplayer.R
import com.zq.zqplayer.bean.RoomInfoBean
import com.zq.zqplayer.databinding.FragmentNewRecommendBinding
import com.zq.zqplayer.mvp.live.ui.LiveActivity
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class NewRecommendFragment: BaseFragment(R.layout.fragment_new_recommend) {

    val mViewModel: RecommendViewModel by viewModels()

    private val binding : FragmentNewRecommendBinding by binding()
    var adapter: NewRecommendAdapter? = null

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initData(savedInstanceState)
    }

    fun initData(savedInstanceState: Bundle?) {
        binding?.lifecycleOwner = this // 设置了这个， livedata 的变化才会应用到界面
        adapter = NewRecommendAdapter(ArrayList())
        initRecycleView()
        initListener()

        val recommendBannerList:ArrayList<Int> = arrayListOf()
        recommendBannerList.add(R.drawable.home_recommend_live_app_1523155786)
        recommendBannerList.add(R.drawable.home_recommend_live_app_1526869950)
        recommendBannerList.add(R.drawable.home_recommend_live_app_1530243925)
        recommendBannerList.add(R.drawable.home_recommend_live_app_1540959428)
        binding?.banner?.autoRollViewPager!!.adapter = object : AutoRollViewPager.RollViewPagerAdapter<Int>(recommendBannerList) {
            override fun getItemLayoutRes(): Int {
                return R.layout.item_in_recommend_banner
            }

            override fun onBindItemView(rootView: ViewGroup?, position: Int, realPosition: Int) {
                val imageUrl:Int = getItem(realPosition)
                val ivContent: ImageView = rootView!!.findViewById(R.id.iv_content)
                ivContent.setImageResource(imageUrl)
            }
        }


        binding?.smartRefreshLayout?.setOnRefreshListener {
            mViewModel.getZqVideoList()
        }
        binding?.smartRefreshLayout?.setOnLoadMoreListener {
            mViewModel.getZqVideoList(false)
        }
        binding?.smartRefreshLayout?.autoRefresh()
    }

    fun initRecycleView(): Unit {
        binding?.recycleView?.adapter = adapter
        val layoutManager: GridLayoutManager = GridLayoutManager(context, 2)
        layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
            override fun getSpanSize(position: Int): Int {
                return 1
            }
        }
        binding?.recycleView?.layoutManager = layoutManager
        adapter!!.mOnActionListener = object : NewRecommendAdapter.OnActionListener {
            override fun onLiveItemClick(item: RoomInfoBean) {
                LiveActivity.openVideo(context, item.live_url, item.room_name)
            }
        }
    }

    fun initListener(): Unit {
        mViewModel.isLoading.observe(viewLifecycleOwner, object : Observer<Boolean> {
            override fun onChanged(t: Boolean?) {
                if(t!!){
//                    showLoading()
                }else{
//                    hideLoading()
                }
            }
        })

        mViewModel.toastMsg.observe(viewLifecycleOwner, object : Observer<String> {
            override fun onChanged(t: String?) {
                Toast.makeText(context, t, Toast.LENGTH_LONG).show()
            }
        })

        mViewModel.onRefreshItems.observe(viewLifecycleOwner, object : Observer<ArrayList<RoomInfoBean>> {
            override fun onChanged(t: ArrayList<RoomInfoBean>?) {
                adapter?.data?.clear()
                adapter?.data?.addAll(t!!)
                adapter?.notifyDataSetChanged()
                binding?.smartRefreshLayout?.finishRefresh()
                binding?.smartRefreshLayout?.finishLoadMore()
                binding?.smartRefreshLayout?.isEnableLoadMore = t!!.isNotEmpty()
            }
        })

        mViewModel.onLoadMoreItems.observe(viewLifecycleOwner, object : Observer<ArrayList<RoomInfoBean>> {
            override fun onChanged(t: ArrayList<RoomInfoBean>?) {
                adapter?.data?.addAll(t!!)
                adapter?.notifyDataSetChanged()
                binding?.smartRefreshLayout?.finishRefresh()
                binding?.smartRefreshLayout?.finishLoadMore()
                binding?.smartRefreshLayout?.isEnableLoadMore = t!!.isNotEmpty()
            }
        })

    }


}